package com.neugh.controller;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
public class Wuzhifubao {

    @RequestMapping("/WuwapPayServlet")
    public String doWapPay(
            HttpSession session,
            @RequestParam(value ="realFee")Double realFee,
            @RequestParam(value ="totall")Double totall,
            @RequestParam(value = "realBille")Double realBille,
            @RequestParam(value = "realWatere")Double realWatere
             ){

        //获得初始化的AlipayClient,将上面创建的配置类中的变量设置到该对象中

        AlipayClient alipayClient =new DefaultAlipayClient(WuAlipayConfig.gatewayUrl, WuAlipayConfig.app_id, WuAlipayConfig.merchant_private_key,

                "json", WuAlipayConfig.charset, WuAlipayConfig.alipay_public_key, WuAlipayConfig.sign_type);

        AlipayTradeWapPayRequest alipayRequest =new AlipayTradeWapPayRequest();//创建API对应的request

        alipayRequest.setReturnUrl(WuAlipayConfig.return_url);

        alipayRequest.setNotifyUrl(WuAlipayConfig.notify_url);

        //如果想正常调用接口则需要传一些必要参数,out_trade_no:订单号,保证唯一性,支付宝根据该参数生成你的支付单号,total_amount:需要支付的金额,注意是String型,且金额计数的分割","不能存在(例如1,000就是错误的参数),subject:对物品进行描述,product_code:是支付类型.更多详细的参数信息参考链接: [https://docs.open.alipay.com/api_1/alipay.trade.page.pay](https://docs.open.alipay.com/api_1/alipay.trade.page.pay)
        int orderId =(int)((Math.random()*9.0D+1.0D)*100000.0D);//自己随便的订单号
        //   total="555";

        session.setAttribute("totall",totall);
        session.setAttribute("realFee",realFee);
        session.setAttribute("realBille",realBille);
        session.setAttribute("realWatere",realWatere);
        System.out.println(realFee);
        System.out.println(realBille);
        System.out.println(realWatere);
        System.out.println(totall);
        try {

            alipayRequest.setBizContent("{\"out_trade_no\":\""+ orderId +"\","

                    +"\"total_amount\":\""+totall +"\","

                    +"\"subject\":\""+ "物业服务" +"\","

                    +"\"product_code\":\"QUICK_WAP_PAY\"}");

            // FAST_INSTANT_TRADE_PAY PC

            // QUICK_WAP_PAY mobile

            //请求

            String result;

            //发送请求并返回

            result = alipayClient.pageExecute(alipayRequest).getBody();

            System.out.println("*********************\n返回结果为："+result);

            return result;

        }catch (Exception e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

            return null;

        }


    }

}
